# -*- coding: utf-8 -*-
class ArticleController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:articleList,:newArticle,:editArticle]

  def articleList
    
  end

  def newArticle

  end

  def editArticle
    
  end

  def create
    article = Article.new
    article.company_id =  session[:company_id]
    article.department_id =  session[:department_id]
    article.employee_id =  session[:employee_id]
    article.title = params[:title]
    article.content = params[:content]
    article.sub_type = params[:sub_type]
    article.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def update
    begin
      article = Article.find(params[:id])
      article.title = params[:title]
      article.content = params[:content]
      article.sub_type = params[:sub_type]
      article.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def getAllArticles
    start = params[:start]
    limit = params[:limit]
    articles = Article.all
    if start.nil?
      articles = articles
    else
      articles = Article.paginate start,limit
    end
    jsonStr="{totalProperty:"+Article.count.to_s+",root:"+articles.to_json(:include=>[:editor])+"}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def getArticleById
    article = Article.find(params[:id])
    rootStr = article.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end
  
end
